package com.mop.dinnerbook.dao;

import java.sql.SQLException;
/*
 订餐记录操作类
 */
public class RecordDAO extends BaseDAO{
    /*
     存储订餐记录
     * 如果没有订餐记录，则插入记录
     * 如果已经存在记录，则更新记录的订餐状态
     * 同时记录下操作的IP记录
     */
	public boolean bookDinner(int mid,String actionIp){
		String usql = "update record set checked = 1,actionIp ='"+actionIp+"' where mid = " + mid +" and rdate >= curdate()";
		String isql = "insert into record(rid,mid,rdate,checked,actionIp)  values(NULL,"+mid+",now(),1,'"+actionIp+"')";
		boolean result = false;
		try {
			int rows = executeUpdate(usql);
			if(rows == 0){
				if(executeInsert(isql) > 0){
					result = true;
				};
			}else{
				result = true;
			}
		} catch (SQLException e) {
                        System.out.println(usql);
                        System.out.println(isql);
			e.printStackTrace();
		}
		return result;
	}
	/*
     存储取消订餐记录
     * 如果没有订餐记录，则插入记录
     * 如果已经存在记录，则更新记录的订餐状态
     * 同时记录下操作的IP记录
     */
	public boolean cancelDinner(int mid,String actionIp){
		String usql = "update record set checked = 0,actionIp ='"+actionIp+"' where mid = " + mid +" and rdate >= curdate()";
		String isql = "insert into record(rid,mid,rdate,checked,actionIp)  values(NULL,"+mid+",now(),1,'"+actionIp+"')";
		boolean result = false;
		try {
			int rows = executeUpdate(usql);
			if(rows == 0){
				if(executeInsert(isql) > 0){
					result = true;
				};
			}else{
				result = true;
			}
		} catch (SQLException e) {
                        System.out.println(usql);
                        System.out.println(isql);
			e.printStackTrace();
		}
		return result;
	}
}
